package com.saicent.note.controller.ws;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.saicent.note.model.User;
import com.saicent.support.authority.Authority;
import com.saicent.support.web.mvc.controller.spring.AnnotationControllerSupport;
import com.saicent.support.web.ws.rs.Response;

@Controller("UserController")
@RequestMapping(value="user")
public class UserController extends AnnotationControllerSupport {

	@RequestMapping("login.ws")
	@ResponseBody
	public Response login(
		@RequestParam("username") String username,
		@RequestParam("password") String password) {
		
		if (username.equals("superadmin") && password.equals("201105272")) {
			User user = new User(username, password);
			user.setAuthority(Authority.topAuthority());
			sessionAttribute("user", user);
			return Response.build(Response.RESPONSE_STATUS_OK);
		}
		
		if (username.equals("admin") && password.equals("201105272")) {
			User user = new User(username, password);
			user.setAuthority(Authority.generateAuthority(1));
			sessionAttribute("user", user);
			return Response.build(Response.RESPONSE_STATUS_OK);
		}
		
		return Response.build(Response.RESPONSE_STATUS_ERROR);
		
	}
	
	@RequestMapping("logout.ws")
	@ResponseBody
	public Response logout() {
		sessionAttribute("user", null);
		return Response.build(Response.RESPONSE_STATUS_OK);
	}
}
